package com.lemon.components.ant.util;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.beans.TableColumns;

public class ClassUtil {

	public static String classpath2filepath(String classpath){
		return classpath.replaceAll("\\.", "\\\\");
	}
	public static String filepath2classpath(String classpath){
		return classpath.replaceAll("\\\\", "\\.").replaceAll("\\/", "\\.");
	}
	public static void setType(List<TableColumns> columnList,String clazz){
		//最好能做成可变的
		String classpath = "com.lemon.data.model.";
		PropertyDescriptor[] propertyDescriptors = getClassDescriptor(classpath+StringUtils.capitalize(clazz));
		for(TableColumns column:columnList){
			for(PropertyDescriptor pd:propertyDescriptors){
				if(pd.getDisplayName().equalsIgnoreCase(column.getCode())){
					column.setType(pd.getPropertyType().getSimpleName());
				}
			}
		}
	}
	
	public static PropertyDescriptor[] getClassDescriptor(String classpath){
		Class<?> clazz = null;
		try {
			clazz = Class.forName(classpath);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		PropertyDescriptor[] property = PropertyUtils.getPropertyDescriptors(clazz);
		for(PropertyDescriptor p:property){
			if(p.getDisplayName().equalsIgnoreCase("class")){
				property = ArrayUtils.removeElement(property, p);
			}
		}
		return property;
	}
	
	public static String getMethodName(String classpath,String filed){
		Class<?> clazz = null;
		try {
			clazz = Class.forName(classpath);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		PropertyDescriptor[] property = PropertyUtils.getPropertyDescriptors(clazz);
		for(PropertyDescriptor p:property){
			if(p.getDisplayName().equalsIgnoreCase(filed)){
				property = ArrayUtils.removeElement(property, p);
				return PropertyUtils.getReadMethod(p).getName();
			}
		}
		return null;
	}
	
	/* 
     * 取得某一类所在包的所有类名 不含迭代 
     */  
    public static String[] getPackageAllClassName(String classLocation, String packageName){  
        //将packageName分解  
        String[] packagePathSplit = packageName.split("[.]");  
        String realClassLocation = classLocation;  
        int packageLength = packagePathSplit.length;  
        for(int i = 0; i< packageLength; i++){  
            realClassLocation = realClassLocation + File.separator+packagePathSplit[i];  
        }  
        File packeageDir = new File(realClassLocation);  
        if(packeageDir.isDirectory()){  
            String[] allClassName = packeageDir.list();  
            return allClassName;  
        }  
        return null;  
    } 
	
	public static void main(String[] args) {
			PropertyDescriptor[] property = getClassDescriptor("com.lemon.data.model.HipacItems");
			for(PropertyDescriptor p:property){
				System.out.println("private "+p.getPropertyType().getSimpleName()+" "+p.getDisplayName()+";");
//				System.out.println(p.getWriteMethod().getModifiers()+" "+p.getWriteMethod().getName());
			}
	}
}
